/**
 * Created by mooshroom on 2016/5/24.
 */
avalon.ready(IfSo_Start())
function IfSo_Start(){
    require([
        '../../plugins/shortcut/shortcut.js',
        '../../lib/whatInput/whatInput.js'
    ], function () {
        var vm=avalon.define({
            $id:"index",
            ready: function () {


                for(var i=0;i<3;i++){
                    vm.addThing()
                }

                for(var i=0;i<2;i++){
                    vm.addMind()
                }

                //vm.$watch('things', function () {
                //    cache.go({
                //
                //    })
                //})

                avalon.scan()
            },


            //第一部
            things:[

            ],
            addThing: function () {
                vm.things.push({
                    title:"",
                    weight:50,
                })

                ForEach(vm.minds, function (obj) {
                    obj.val.push('')
                })



            },
            delThing: function ($index) {
                vm.things.splice($index,1)
                ForEach(vm.minds, function (obj) {
                    obj.val.splice($index,1)
                })

            },

            //第二部
            minds:[],

            addMind: function () {
                var val=[]
                ForEach(vm.things, function () {
                    val.push('')
                })

                vm.minds.push({
                    title:'',
                    val:val,
                    happiness:0,
                })

            },
            delMind: function (index) {
                vm.minds.splice(index,1)


            },


            //计算
            bigest:-1,
            addUp: function () {
                ForEach(vm.minds, function (el,i) {
                    el.happiness=0
                    ForEach(el.val, function (al,index) {
                        el.happiness=Number(Number(el.happiness+al*vm.things[index].weight).toFixed(4))
                    })

                    if(vm.bigest<0){
                        vm.bigest=i
                        return
                    }

                    if(vm.minds[vm.bigest].happiness<el.happiness){
                        vm.bigest=i
                        return
                    }
                })
            }


        })
        vm.ready()
        return window[vm.$id]=vm
    })
}

function ForEach(obj, func) {
    if(typeof obj=="object"){
        if(obj.length==undefined){
            for (var i in obj) {
                func(obj[i],i);
            }
        }else{
            for (var i = 0; i < obj.length; i++) {
                func(obj[i],i);
            }
        }
    }else{
        console.log('类型错误:'+JSON.stringify(obj))
    }
}